import baseService from "@/plugins/axios.js";
import store from "@/store/index.js";
export default class BaseApi {
  doRequest(url, method, params, data, authorization, contentType) {
    // console.log(data);
    const headers = {
      "Content-Type": contentType,
    };
    if (authorization) {
      const token = store.getters["role/token"];
      if (!token) {
        return Promise.reject("非法请求！");
      } else {
        headers.Authorization = 'Bearer ' + token;
      }
    }
    return baseService({
      method,
      url: url,
      params: params,
      data: data,
      headers,
      responseType: "json",
      // 允许为上传处理进度事件
      onUploadProgress: function (progressEvent) {
        // console.log("progressEvent", progressEvent);
      },
      // 允许为下载处理进度事件
      onDownloadProgress: function (progressEvent) {
        // console.log("progressEvent", progressEvent);
      },
    });
  }
  get({ url, params = {}, data = {}, authorization = true, contentType = "application/json" }) {
    return this.doRequest(url, "get", params, data, authorization, contentType);
  }
  post({ url, params = {}, data = {}, authorization = true, contentType = "application/json" }) {
    return this.doRequest(url, "post", params, data, authorization, contentType);
  }
  postImg({ url, params = {}, data = {}, authorization = true, contentType = "multipart/form-data" }) {
    return this.doRequest(url, "post", params, data, authorization, contentType);
  }
}

export const api = new BaseApi();
